<template>
  <div>
    <istable :logo="list" :page="doot" @pagepath="pageAPI" @addmen="vuensk" @delcard="deluser" @changed="edit" />
    <fromis ref="info" :valeb="permissionIds" :isshow="dialogFormVisible" :isform="from" :rules="isrules" :menus="menu" :andnew="isnew" @addseve="addmenu" />
  </div>
</template>

<script >
import { findRolesByPage, removeRole, findAllPermissions, addsave, findRolePermissions } from '@/api/contro/index'
import istable from './table/index'
import fromis from './controfrom/idnex'
export default {
  components: { istable, fromis },
  data() {
    return {
      menu: [],
      doot: {
        start: 1,
        limit: 10,
        total: 0
      },
      permissionIds: [],
      list: [],
      dialogFormVisible: false,
      from: {
        permissionIds: [], // 权限id集合
        remark: '', // 备注
        roleCode: '', // 角色编码
        roleName: '' // 角色名称
      },
      isnew: true,
      isrules: {
        roleName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ] }
    }
  },
  computed: {},
  created() {
    this.findRolesByPageAPI()
    findAllPermissions().then(res => {
      console.log(res)
      if (res.success) {
        this.menu = res.data.menus
      }
    })
  },

  mounted() {},

  methods: {
    // 编辑权限
    edit(row) {
      console.log(row)
      findRolePermissions(row.id).then(res => {
        console.log(res)
        if (res.success) {
          this.isnew = true
          this.from = res.data.role
          this.dialogFormVisible = true
          this.permissionIds = res.data.role.permissionIds
          // this.$refs.info.setCheckedNodes()
        }
      })
    },
    // 新增用户
    addmenu(val) {
      console.log(val)
      this.from = val
      addsave({ ...this.from }).then(res => {
        console.log(res)
        if (res.success) {
          this.$message.success('添加成功')
          this.dialogFormVisible = false
          this.isnew = true
        } else {
          this.$message.error('添加失败')
        }
      })
    },
    // 新增
    vuensk(row) {
      this.dialogFormVisible = true
    },
    findRolesByPageAPI() {
      var _this = this
      findRolesByPage(this.doot.start, this.doot.limit).then(res => {
        if (res.success) {
          _this.list = res.data.rows
          _this.doot.total = res.data.total
        }
      })
    },
    // 分页切换
    pageAPI(row) {
      var _this = this
      findRolesByPage(row.start, row.limit).then(res => {
        if (res.success) {
          _this.list = res.data.rows
          _this.doot.total = res.data.total
        }
      })
    },
    // 删除
    deluser(row) {
      removeRole(row).then(res => {
        if (res.success) {
          this.$message.success('删除成功')
          this.findRolesByPageAPI()
        } else {
          this.$message.error('删除失败')
        }
      })
    }
  }
}

</script>
<style lang="scss" scoped>

</style>
